<?php
//require_once("admin/includes/Response.php");

class picture
{


//获取文件后缀名函数
    public static function fileext($filename)
    {
        return substr(strrchr($filename, '.'), 1);
    }

//生成随机文件名函数
    public static function random($length)
    {
        $hash = 'CR-';
        $chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz';
        $max = strlen($chars) - 1;
        mt_srand((double)microtime() * 1000000);
        for ($i = 0; $i < $length; $i++) {
            $hash .= $chars[mt_rand(0, $max)];
        }
        return $hash;
    }

    public static function uploaddir($c, $b)
    {
        $uploaddir = ROOT_PATH."images/dober/";
        $type = array("jpg", "gif", "bmp", "jpeg", "png");//设置允许上传文件的类型
        $count = count($c);
        //echo "1".$count."....";
        for ($i = 0;$i<$count;$i++) {
            $a = strtolower(self::fileext($c[$i]));
//判断文件类型
            //echo $c[$i];


            if (!in_array($a, $type)) {
                $text = implode(",", $type);
                echo "您只能上传以下类型文件: ", $text, "<br>";
                return Response::json(410, "您只能上传" . $text . "类型的文件");
            } //生成目标文件的文件名
            else {
                $filename = explode(".", $c[$i]);

                do {
                    $filename[0] = self::random(10); //设置随机数长度
                    $name = implode(".", $filename);
                    $uploadfile[$i] = $uploaddir . $name;
                } while (file_exists($uploadfile[$i]));
                echo $c[$i];
                if (is_uploaded_file($b[$i])) {
                    if (move_uploaded_file($b[$i], $uploadfile[$i])) {

                        return $uploadfile[$i];
                        exit();
                    } else {
                        echo "shibai";
//                        return Response::json(407, "上传失败", $uploadfile[$i]);
                    }
                }
            }
        }
    }
}
?>